package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.service.MarketKeywordService;
import com.cskaoyan.market.util.HttpUtil2;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RequestMapping("/admin/keyword")
@RestController
public class MarketKeywordController{

    // 【父接口接收
    @Autowired
    MarketKeywordService marketKeywordService;

    @GetMapping("/list")
    @RequiresPermissions("admin:keyword:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "关键词"},button = "查询")
    public Object list(HttpServletRequest req) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        // 选择框
        String keyword = req.getParameter("keyword");
        String url = req.getParameter("url");

        List<MarketKeyword> marketKeywordList = marketKeywordService.list(page, limit, sort, order, keyword, url);

        return ResponseUtil.okList(marketKeywordList);

    }

    @PostMapping("/update")
    @RequiresPermissions("admin:keyword:update")
    @RequiresPermissionsDesc(menu = {"商场管理", "关键词"},button = "更新")
    public Object update(HttpServletRequest req) throws IOException {
        String requestBody = HttpUtil2.getRequestBody(req);
        Integer id = JacksonUtil.parseInteger(requestBody, "id");
        String keyword = JacksonUtil.parseString(requestBody, "keyword");
        String url = JacksonUtil.parseString(requestBody, "url");
        Boolean isHot = JacksonUtil.parseBoolean(requestBody, "isHot");
        Boolean isDefault = JacksonUtil.parseBoolean(requestBody, "isDefault");
        Integer sortOrder = JacksonUtil.parseInteger(requestBody, "sortOrder");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");
        Boolean deleted = JacksonUtil.parseBoolean(requestBody, "deleted");

        MarketKeyword marketKeyword = marketKeywordService.update(id,keyword,url,isHot,isDefault,sortOrder,updateTime,deleted);

        return ResponseUtil.ok(marketKeyword);
    }

    @PostMapping("/create")
    @RequiresPermissions("admin:keyword:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "关键词"},button = "新增")
    public Object create(HttpServletRequest req) throws IOException {
        String requestBody = HttpUtil2.getRequestBody(req);
        String keyword = JacksonUtil.parseString(requestBody, "keyword");
        String url = JacksonUtil.parseString(requestBody, "url");
        Boolean isHot = JacksonUtil.parseBoolean(requestBody, "isHot");
        Boolean isDefault = JacksonUtil.parseBoolean(requestBody, "isDefault");

        // 判空 // 好像前端就已经有判断了
        MarketKeyword marketKeyword = marketKeywordService.create(keyword, url, isHot, isDefault);

        return ResponseUtil.ok(marketKeyword);
    }

    @PostMapping("/delete")
    @RequiresPermissions("admin:keyword:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "关键词"},button = "删除")
    public Object delete(HttpServletRequest req) throws IOException {
        String requestBody = HttpUtil2.getRequestBody(req);
        String id = JacksonUtil.parseString(requestBody, "id");

        int returnCode = marketKeywordService.delete(id);

        // {"errno":0,"errmsg":"成功"}
        if (returnCode == 0) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.fail();
        }
    }
}
